package demo_base.bean;

import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class Test {
    public static void main(String[] args) {
        // 输出，bean 对象转为xml文件
        try (OutputStream outputStream = new FileOutputStream("src/demo_base/bean/b.xml")) {
            User user = new User();
            user.setName("yoke");
            user.setAge(10);
            XMLEncoder encoder = new XMLEncoder(outputStream);
            encoder.writeObject(user);
            encoder.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 输入，xml文件转为bean对象
        try (InputStream inputStream = new FileInputStream("src/demo_base/bean/a.xml")) {
            XMLDecoder decoder = new XMLDecoder(inputStream);
            User user = (User) decoder.readObject();
            System.out.println(user.getName() + ":" + user.getAge());
            decoder.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

